﻿#region namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

using SLIITPro.Core;
using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

#endregion

namespace SLIITPro.SysUsers
{
    public partial class ContentEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSave.Click += new EventHandler(btnSave_Click);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            if (!Page.IsPostBack)
                LoadBusiness();
        }

        private Users _users;
        private void LoadBusiness()
        {
            _businessItemExtension = new BusinessItemExtension();
            var business = _businessItemExtension.SelectBusiness();
            if (Page.User.IsInRole("administrator") || Page.User.IsInRole("content"))
            {
                grdContent.DataSource = business;
            }
            else
            {
                _users = new Users(new UserExtension().SelectUser("Email", Page.User.Identity.Name).Rows[0]);
                var sortedList = (from businessItem in business
                                  where businessItem.BUId == _users.UserId
                                  select businessItem).ToList();
                grdContent.DataSource = sortedList;
            }
            grdContent.DataBind();
            UpdatePanel1.Update();
        }

        private BusinessItem _businessItem;
        void btnDelete_Click(object sender, EventArgs e)
        {
            _businessItemExtension = new BusinessItemExtension();
            var table = _businessItemExtension.SelectBusiness(int.Parse(dvBusinessDetail.DataKey.Value.ToString()));
            _businessItem = new BusinessItem(table.Rows[0]);
            _businessItemExtension.DeleteBusinessItem(_businessItem.BId);

            string path = Server.MapPath(CoreSettings.GlobalBusinessImageRoot + _businessItem.BImageFolderID + "/");
            _businessItemExtension.DeleteImageFolder(path.Trim());

            this.dvBusinessDetail.Visible = false;
            this.mdlPopup.Hide();
            LoadBusiness();
            this.UpdatePanel1.Update();
        }

        protected void grdContent_IndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadBusiness();
            grdContent.PageIndex = e.NewPageIndex;
            grdContent.DataBind();
        }

        protected void grdContent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void grdContent_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dvBusinessDetail.Visible = true;
            LoadBusiness();
            this.updPnlBusinessDetail.Update();
            this.mdlPopup.Show();
        }

        protected void SqlDataSourceDetails_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters[0].Value = Convert.ToString(grdContent.DataKeys[grdContent.SelectedIndex].Value);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            var lable = (Label)dvBusinessDetail.Rows[9].FindControl("lblUpdate");
            lable.Text = DateTime.Now.ToString();

            dvBusinessDetail.UpdateItem(false);
            this.dvBusinessDetail.Visible = false;

            this.mdlPopup.Hide();
            LoadBusiness();
        }

        private BusinessItemExtension _businessItemExtension;
        private CommentExtension _commentExtension;
        protected void btnMultiDelete_Click(object sender, EventArgs e)
        {
            _businessItemExtension = new BusinessItemExtension();
            _commentExtension = new CommentExtension();
            foreach (GridViewRow row in grdContent.Rows)
            {
                try
                {
                    var chkbx = (CheckBox)row.FindControl("chkDelete");
                    if (chkbx != null && chkbx.Checked)
                    {
                        _businessItem = new BusinessItem(_businessItemExtension.SelectBusiness((int)grdContent.DataKeys[row.RowIndex].Value).Rows[0]);
                        _businessItemExtension.DeleteBusinessItem(_businessItem.BId);
                        var path = Server.MapPath(CoreSettings.GlobalBusinessImageRoot + _businessItem.BImageFolderID + "/");
                        _businessItemExtension.DeleteImageFolder(path.Trim());
                        _commentExtension.DeleteCommentByBusinessId(_businessItem.BId);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            LoadBusiness();
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grdContent.Rows)
            {
                var chkbx = (CheckBox)row.FindControl("chkDelete");
                if (chkbx == null) continue;
                chkbx.Checked = !chkbx.Checked;
                btnSelectAll.Text = chkbx.Checked ? "Deselect All" : "Select All";
            }
        }
    }
}
